package com.devilvan.betrice.lol.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.devilvan.betrice.lol.dto.LolBattleHeaderDto;
import com.devilvan.betrice.lol.po.LolBattleHeaderPo;
import com.devilvan.betrice.lol.service.LolBattleHeaderService;
import com.devilvan.betrice.lol.mapper.LolBattleHeaderMapper;
import com.devilvan.betrice.lol.repository.LolBattleHeaderRepository;
import com.devilvan.betrice.util.UEmpty;
import com.devilvan.betrice.util.UCopy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Evad.Wu
 * @Description LOL-对局基本信息 业务逻辑类
 * @date 2022-09-30
 */
@Service
public class LolBattleHeaderServiceImpl extends ServiceImpl<LolBattleHeaderMapper, LolBattleHeaderPo> implements LolBattleHeaderService {
    @Resource(name = "lolBattleHeaderRepository")
    private LolBattleHeaderRepository lolBattleHeaderRepository;

    @Override
    public LolBattleHeaderPo saveUpdate(LolBattleHeaderDto lolBattleHeaderDto) {
        LolBattleHeaderPo lolBattleHeaderPo = new LolBattleHeaderPo();
        UCopy.fullCopy(lolBattleHeaderDto, lolBattleHeaderPo);
        return lolBattleHeaderRepository.saveAndFlush(lolBattleHeaderPo);
    }

    @Override
    public List<LolBattleHeaderPo> saveUpdateBatch(List<LolBattleHeaderDto> list) {
        List<LolBattleHeaderPo> lolBattleHeaderPoList = UCopy.fullCopyCollections(list, LolBattleHeaderPo.class);
        return lolBattleHeaderRepository.saveAllAndFlush(lolBattleHeaderPoList);
    }

    @Override
    public List<Long> findDisjunctionByGameIdIn(List<Long> gameIdList) {
        LambdaQueryWrapper<LolBattleHeaderPo> queryWrapper = new QueryWrapper<LolBattleHeaderPo>().lambda();
        queryWrapper.select(LolBattleHeaderPo::getGameId);
        queryWrapper.in(LolBattleHeaderPo::getGameId, gameIdList);
        List<LolBattleHeaderPo> list = this.list(queryWrapper);
        if (UEmpty.isEmpty(list)) {
            return gameIdList;
        }
        List<Long> exsistGameIdList = list.stream().map(LolBattleHeaderPo::getGameId).toList();
        return new ArrayList<>(CollUtil.disjunction(gameIdList, exsistGameIdList));
    }
}
